package com.chezhujie.www.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.chezhujie.www.domain.Tclause;
import com.chezhujie.www.service.TclauseService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xy.core.page.Pagination;
import com.xy.user.base.controller.BaseController;




@Controller
@RequestMapping("clause")
public class TclauseController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(TclauseController.class);
	public TclauseController() {
	}
 
 
 	/**
	 * 条款内容（t_clause)新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("tclauseAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tclause", new Tclause());
      setDict(model);
		return "clause/tclauseEdit";
	}

	/**
	 * 条款内容（t_clause)保存
	 * 
	 * @param tclause
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("tclauseSave.do")
	public String save(Tclause tclause, HttpServletRequest request, ModelMap model) throws Exception{
      
		try {
			if(tclause.getClauseId()!=null&&!tclause.getClauseId().equals("")){
				// 编辑
				tclauseService.updateTclause(tclause);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑条款内容（t_clause)", "clauseId="+tclause.getClauseId()));
			} else {
				// 新增
				tclauseService.insertTclause(tclause);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增条款内容（t_clause)", "clauseId="+tclause.getClauseId()));
			}
   		} catch (Exception e) {
   			log.error("tclause save error:", e); 
            throw e;
   		}
      
		return "redirect:tclauseList.do";
	}

	/**
	 * 条款内容（t_clause)删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("tclauseDelete.do")
	public String delete(@RequestParam("ids") List<String> clauseIdList) throws Exception{
		try {
         if (clauseIdList != null) {
   			tclauseService.deleteTclause(clauseIdList);
   		}
         } catch (Exception e) {
   			log.error("tclause delete error:", e); 
            throw e;
   		}
		return "redirect:tclauseList.do";
	}
	 
	/**
	 * 条款内容（t_clause)编辑页面
	 * 
	 * @param clauseId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("tclauseEdit.do")
	public String edit(String clauseId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("clauseId", clauseId);
         Tclause tclause = tclauseService.getTclause(param);
   		model.addAttribute("tclause", tclause);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("tclause edit error:", e); 
            throw e;
   		}
		return "clause/tclauseEdit";
	}

	/**
	 * 条款内容（t_clause)详情
	 * 
	 * @param clauseId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("tclauseDetail.do")
	public String detail(String clauseId, HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception{
      try {
      		Map<String, Object> param = new HashMap<String, Object>();
      		param.put("clauseId", clauseId);
            Tclause tclause = tclauseService.getTclause(param);
      		model.addAttribute("tclause", tclause);
            setDict(model);
            } catch (Exception e) {
   			log.error("tclause detail error:", e); 
            throw e;
   		  }
		return "clause/tclauseView";
	}
	 
	/**
	 * 条款内容（t_clause)查询（分页）
	 * 
	 * @param tclause
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("tclauseList.do")
	public String listPage(Tclause tclause, Integer pageNo, String order,
			String sort,HttpServletRequest request, ModelMap model) throws Exception{
      try {   
   		if (pageNo == null)
   			pageNo = 1;
         Pagination pagination = tclauseService.getTclauseList(toMap(tclause),20 , pageNo,
   				order, sort);          
   		model.addAttribute("pagination", pagination);
         setDict(model);
         } catch (Exception e) {
			log.error("tclause listPage error:", e); 
         throw e;
		   }
		return "clause/tclauseList";
	}
	
	/**
	 * 条款内容（t_clause)查询（分页）(不能添加重复类型条目)
	 * 
	 * @param tclause
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("tclauseListAdd.do")
	public void listPage(Tclause tclause, Integer pageNo, String order,
			String sort,HttpServletRequest request,HttpServletResponse response) throws Exception{
      try {   
   		if (pageNo == null)
   			pageNo = 1;
   		    String type = request.getParameter("type");
   		     if(type!=null && !type.equals("")){
   		    	 tclause.setType(Integer.valueOf(type));
   		     }
         Pagination pagination = tclauseService.getTclauseList(toMap(tclause), 20, pageNo,
   				order, sort);  
			Map<String, Object> result = new HashMap<String, Object>();
	   		result.put("totalCounts", pagination.getTotalCount()) ;
	   		result.put("list", pagination.getList());
	   		//responseSucess(result,true, response);
         } catch (Exception e) {
 			log.error("tshopcontent  listPage error:", e); 
 			//responseError(e, response);
		   }		
	}	
	
	 
	/**
	 * 条款内容（t_clause)查询
	 * 
	 * @param tclause
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("tclauseList1.do")
	public String list(Tclause tclause, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String,Object> map = tclauseService.getTclauseList(toMap(tclause));
   		model.addAttribute("list", map.get("rows"));
   		model.addAttribute("total", map.get("total"));
         setDict(model);
         } catch (Exception e) {
   			log.error("tclause list error:", e); 
            throw e;
   		}
		return "clause/tclauseList";
	}
	 

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tclause
	 * @return
	 */	private Map<String,Object> toMap(Tclause tclause){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tclause.getClauseId()!=null&&!tclause.getClauseId().equals("")){
			map.put("clauseId", tclause.getClauseId());
		}
		if(tclause.getType()!=null&&!tclause.getType().equals("")){
			map.put("type", tclause.getType());
		}
		if(tclause.getContent()!=null&&!tclause.getContent().equals("")){
			map.put("content", tclause.getContent());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}



   
      
	@Resource
	private TclauseService tclauseService;


}